﻿Imports Entidades
Imports Microsoft.Office.Interop.Excel
Public Class frmObjetosPorFamilia
    Private fam As List(Of Familia)
    Private Sub btnConsultar_Click(sender As Object, e As EventArgs) Handles btnConsultar.Click
        If cboFamilias.Text = "" Then
            MessageBox.Show("No hay ninguna familia seleccionada")
            Exit Sub
        End If
        Dim lista As List(Of Objeto) = miGestion.objetosPorFamilia(fam(cboFamilias.SelectedIndex))
        DataGridView1.DataSource = ""

        DataGridView1.DataSource = lista
        DataGridView1.Columns("IdFamilia").Visible = False
        DataGridView1.Columns("NumeroFamilia").Visible = False
        DataGridView1.Columns("IdEntrega").Visible = False
        DataGridView1.Columns("NumeroSerie").DisplayIndex = 0
        DataGridView1.Columns("Descripcion").DisplayIndex = 1
        DataGridView1.Columns("Tamaño").DisplayIndex = 2
        DataGridView1.Columns("Valoracion").DisplayIndex = 3
        DataGridView1.Columns("dadoDeBaja").Visible = False


    End Sub



    Private Sub frmObjetosPorFamilia_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        fam = miGestion.obtenerFamilias
        For i As Integer = 0 To fam.Count - 1
            cboFamilias.Items.Add(fam(i).Nombre)
        Next
    End Sub

    Function GridAExcel(ByVal ElGrid As DataGridView) As Boolean
        'Creamos las variables
        Dim exApp As New Microsoft.Office.Interop.Excel.Application
        Dim exLibro As Microsoft.Office.Interop.Excel.Workbook
        Dim exHoja As Microsoft.Office.Interop.Excel.Worksheet
        Try
            'Añadimos el Libro al programa, y la hoja al libro
            exLibro = exApp.Workbooks.Add
            exHoja = exLibro.Worksheets.Add()
            ' ¿Cuantas columnas y cuantas filas?
            Dim NCol As Integer = ElGrid.ColumnCount
            Dim NRow As Integer = ElGrid.RowCount
            'Aqui recorremos todas las filas, y por cada fila todas las columnas y vamos escribiendo.
            For i As Integer = 1 To NCol
                exHoja.Cells.Item(1, i) = ElGrid.Columns(i - 1).Name.ToString
                'exHoja.Cells.Item(1, i).HorizontalAlignment = 3
            Next
            For Fila As Integer = 0 To NRow - 1
                For Col As Integer = 0 To NCol - 1
                    exHoja.Cells.Item(Fila + 2, Col + 1) = ElGrid.Rows(Fila).Cells(Col).Value
                Next
            Next
            'Titulo en negrita, Alineado al centro y que el tamaño de la columna se ajuste al texto
            exHoja.Rows.Item(1).Font.Bold = 1
            exHoja.Rows.Item(1).HorizontalAlignment = 3
            exHoja.Columns.AutoFit()
            'Aplicación visible
            exApp.Application.Visible = True
            exHoja = Nothing
            exLibro = Nothing
            exApp = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error al exportar a Excel")
            Return False
        End Try
        Return True
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GridAExcel(DataGridView1)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()

    End Sub
End Class